package com.neu.his.controller.SystemManage01.nonDrugManage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.neu.his.core.Result;
import com.neu.his.entity.NonDrug;
import com.neu.his.entity.VO.SystemManage01.nonDrugManage.NonDrugVO;
import com.neu.his.entity.request.DeleteParams;
import com.neu.his.entity.request.nonDrugManage.NonDrugInsertParams;
import com.neu.his.entity.request.nonDrugManage.NonDrugPageParams;
import com.neu.his.entity.request.nonDrugManage.NonDrugUpdateParams;
import com.neu.his.mapper.NonDrugMapper;
import com.neu.his.utils.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/17 19:18
 */
@RestController
@RequestMapping("/non_drug")
public class NonDrugManageController {
    @Autowired
    NonDrugMapper nonDrugMapper;

    @PostMapping
    public Result insertOne(@RequestBody NonDrugInsertParams nonDrugInsertParams) {
        //先查询是否已有
        NonDrug nonDrug = new NonDrug();
        nonDrug.copy(nonDrugInsertParams);
        nonDrug.setCreateDate();
        QueryWrapper queryWrapper = new QueryWrapper<NonDrug>();
        queryWrapper.eq("non_drug_name", nonDrugInsertParams.getNonDrugName());
        queryWrapper.eq("remove_mark", 1);
        if (!nonDrugMapper.selectList(queryWrapper).isEmpty())
            return Result.fail();
        //再插入
        nonDrugMapper.insert(nonDrug);
        return Result.success();
    }

    @PostMapping("/query")
    public Result nonDrugQuery(@RequestBody NonDrugPageParams nonDrugPageParams) {
        //设置查询对象
        QueryWrapper<NonDrug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(nonDrugPageParams.getNonDrugCode()), "non_drug_code", nonDrugPageParams.getNonDrugCode());
        queryWrapper.eq(StringUtils.isNotEmpty(nonDrugPageParams.getNonDrugName()), "non_drug_name", nonDrugPageParams.getNonDrugName());
        queryWrapper.eq(StringUtils.isNotEmpty(nonDrugPageParams.getNonDrugUnit()), "non_drug_unit", nonDrugPageParams.getNonDrugUnit());
        queryWrapper.eq(StringUtils.isNotEmpty(nonDrugPageParams.getNonDrugCode()), "non_drug_code", nonDrugPageParams.getNonDrugCode());
        queryWrapper.ne("remove_mark", 1L);
        //查询
        List<NonDrug> list = nonDrugMapper.selectList(queryWrapper);
        //装入List<VO>
        List<NonDrugVO> result=new ArrayList<>();
        for (NonDrug nonDrug : list) {
            NonDrugVO nonDrugVO=new NonDrugVO();
            nonDrugVO.setId(nonDrug.getId());
            nonDrugVO.setNonDrugCode(nonDrug.getNonDrugCode());
            nonDrugVO.setNonDrugName(nonDrug.getNonDrugName());
            nonDrugVO.setNonDrugPrice(nonDrug.getNonDrugPrice());
            nonDrugVO.setNonDrugUnit(nonDrug.getNonDrugUnit());
            result.add(nonDrugVO);
        }
        //生成分页
        PageList pageList = new PageList(result, nonDrugPageParams.getCurrentPage(), nonDrugPageParams.getPageSize());
        return Result.success(pageList);
    }

    @PutMapping
    public Result nonDrugUpdateById(@RequestBody NonDrugUpdateParams nonDrugUpdateParams) {
        UpdateWrapper<NonDrug> updateWrapper = new UpdateWrapper();
        updateWrapper.ne("remove_mark", 1);
        updateWrapper.eq("id", nonDrugUpdateParams.getId());
        updateWrapper.set(StringUtils.isNotEmpty(nonDrugUpdateParams.getNonDrugCode()), "non_drug_code", nonDrugUpdateParams.getNonDrugCode());
        updateWrapper.set(StringUtils.isNotEmpty(nonDrugUpdateParams.getNonDrugName()), "non_drug_name", nonDrugUpdateParams.getNonDrugName());
        updateWrapper.set(StringUtils.isNotEmpty(nonDrugUpdateParams.getNonDrugUnit()), "non_drug_unit", nonDrugUpdateParams.getNonDrugUnit());
        updateWrapper.set("update_date", new Date());
        updateWrapper.set("non_drug_price", nonDrugUpdateParams.getNonDrugPrice());
        nonDrugMapper.update(null, updateWrapper);
        return Result.success();
    }

    @DeleteMapping
    public Result nonDrugDelete(@RequestBody DeleteParams deleteParams) {
        UpdateWrapper<NonDrug> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", deleteParams.getIds());
        updateWrapper.set("remove_mark", 1);
        nonDrugMapper.update(null, updateWrapper);
        return Result.success(new ArrayList<Long>());
    }
}
